﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;
using DevComponents.DotNetBar.Controls;
using DevComponents.DotNetBar;
using DevComponents.Editors.DateTimeAdv;
using QLTB.Controllers;
using QuanLyThietBi.Manager;
using QuanLyThietBi.ManagerBase;
using QuanLyThietBi.HamXuLy;
namespace QLTB.Forms
{
    public partial class FrmEditNguoiQuanLy : DevComponents.DotNetBar.Office2007Form
    {
        #region fields

        public bool IsThemMoi = false;
        public EventHandler ThemEvent;
        TBL_NhomQuanLyCtrl m_NhomQuanLy = new TBL_NhomQuanLyCtrl();
        TBL_NguoiQuanLyCtrl m_NguoiQuanLyCtrl = new TBL_NguoiQuanLyCtrl();

        #endregion
        public FrmEditNguoiQuanLy()
        {
            InitializeComponent();
        }
        public FrmEditNguoiQuanLy(string MaQL, bool IsThem_Moi): this()
        {
           IsThemMoi = IsThem_Moi;
           txtMaQuanLy.Text = MaQL;
           if (IsThemMoi)
           {
               m_NhomQuanLy.HienThiComboBoxNhomQuanLy(cbxNhomQL);
           }
           else
           { 
            DataTable dtQuanLy = TBL_NguoiQuanLyManager.CreateInstant().SelectByMaQuanLy(MaQL);
        
               txtTenQuanLy.DataBindings.Add("Text", dtQuanLy, "hoten");
               txtDiachi.DataBindings.Add("Text", dtQuanLy, "DiaChi");
               txtDienthoai.DataBindings.Add("Text", dtQuanLy, "DienThoai");
               txtEmail.DataBindings.Add("Text", dtQuanLy, "Email");
               dtpngaysinh.DataBindings.Add("Text", dtQuanLy, "ngaySinh");
               txtTenDN.DataBindings.Add("Text", dtQuanLy, "TenDangNhap");
               txtMatKhau.DataBindings.Add("Text", dtQuanLy, "MatKhau");
               cbxNhomQL.DataBindings.Add("SelectedValue", dtQuanLy, "MaNhomQuanLy");
               m_NhomQuanLy.HienThiComboBoxNhomQuanLy(cbxNhomQL);
           }
          
        }

        private void FrmEditNguoiQuanLy_Load(object sender, EventArgs e)
        {
            //m_NhomQuanLy.HienThiComboBoxNhomQuanLy(cbxNhomQL);
        }

        private void btnApdung_Click(object sender, EventArgs e)
        {
            if (IsThemMoi)
            {
                if (txtMaQuanLy.Text != "" && txtTenQuanLy.Text != "" && txtDiachi.Text != "" && txtDienthoai.Text != "" && txtEmail.Text != "" && txtTenDN.Text != "" && txtMatKhau.Text != "")
                {
                    HamXuLy GetID = new HamXuLy();
                    m_NguoiQuanLyCtrl.ThemMoiNguoiQuanLy(txtMaQuanLy.Text, txtTenQuanLy.Text, txtDiachi.Text, txtDienthoai.Text, txtEmail.Text, dtpngaysinh.Value.ToString(), txtTenDN.Text, txtMatKhau.Text, cbxNhomQL.SelectedValue.ToString());

                    txtMaQuanLy.Text = GetID.LayMaID("P_GetMaNguoiQL");
                    txtTenQuanLy.Text = "";
                    txtTenQuanLy.Focus();
                    txtDiachi.Text = "";
                    txtDienthoai.Text = "";
                    txtEmail.Text = "";
                    txtTenDN.Text = "";
                    txtMatKhau.Text = "";
                    
                    if (ThemEvent != null)
                        ThemEvent(this, EventArgs.Empty);
                }
                else
                {
                    MessageBoxEx.Show("Giá trị của các ô không được rỗng!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                if (txtMaQuanLy.Text != "" & txtTenQuanLy.Text != "" & txtDiachi.Text != "" & txtDienthoai.Text != "" & txtEmail.Text != "" & txtTenDN.Text != "" & txtMatKhau.Text != "")
                {
                    HamXuLy GetID = new HamXuLy();
                    m_NguoiQuanLyCtrl.CapNhatNguoiQuanLy(txtMaQuanLy.Text, txtTenQuanLy.Text, txtDiachi.Text, txtDienthoai.Text, txtEmail.Text, dtpngaysinh.Value.ToString(), txtTenDN.Text, txtMatKhau.Text, cbxNhomQL.SelectedValue.ToString());
                    if (ThemEvent != null)
                        ThemEvent(this, EventArgs.Empty);
                }
                else
                {
                    MessageBoxEx.Show("Giá trị của các ô không được rỗng!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            {
                if (IsThemMoi)
                {
                    if (txtMaQuanLy.Text != "" && txtTenQuanLy.Text != "" && txtDiachi.Text != "" && txtDienthoai.Text != "" && txtEmail.Text != "" && txtTenDN.Text != "" && txtMatKhau.Text != "")
                    {
                        HamXuLy GetID = new HamXuLy();
                        m_NguoiQuanLyCtrl.ThemMoiNguoiQuanLy(txtMaQuanLy.Text, txtTenQuanLy.Text, txtDiachi.Text, txtDienthoai.Text, txtEmail.Text, dtpngaysinh.Value.ToString(), txtTenDN.Text, txtMatKhau.Text, cbxNhomQL.SelectedValue.ToString());
                        this.Close();

                        if (ThemEvent != null)
                            ThemEvent(this, EventArgs.Empty);
                    }
                    else
                    {
                        MessageBoxEx.Show("Giá trị của các ô không được rỗng!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    if (txtMaQuanLy.Text != "" & txtTenQuanLy.Text != "" & txtDiachi.Text != "" & txtDienthoai.Text != "" & txtEmail.Text != "" & txtTenDN.Text != "" & txtMatKhau.Text != "")
                    {
                        HamXuLy GetID = new HamXuLy();
                        m_NguoiQuanLyCtrl.CapNhatNguoiQuanLy(txtMaQuanLy.Text, txtTenQuanLy.Text, txtDiachi.Text, txtDienthoai.Text, txtEmail.Text, dtpngaysinh.Value.ToString(), txtTenDN.Text, txtMatKhau.Text, cbxNhomQL.SelectedValue.ToString());
                        if (ThemEvent != null)
                            ThemEvent(this, EventArgs.Empty);
                        this.Close();
                    }
                    else
                    {
                        MessageBoxEx.Show("Giá trị của các ô không được rỗng!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnTroGiup_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Thông tin hướng dẫn sử dụng!!!");
        }
    }
}